<?php

Yii::import('common.models.page.*');
Yii::import('common.models.ad.*');

class GoogleadsController extends Controller {

    public $activeMenu = 'cms';

    public function actionCreate() {
	$model = new GoogleAds;
	if (isset($_POST['GoogleAds'])) {
	    $model->attributes = $_POST['GoogleAds'];

	    if ($model->save()) {
		Yii::app()->user->setFlash('success', 'The google ads has been created');
		//redirect to update
		$this->redirect(array('update', 'id' => $model->block_id));
	    } else {
		Yii::app()->user->setFlash('error', 'Sorry, please try again');
	    }
	}

	$this->render('create', array(
	    'model' => $model
	));
    }

    public function actionUpdate($id) {
	$model = $this->_loadModel($id);

	if (isset($_POST['GoogleAds'])) {
	    $model->attributes = $_POST['GoogleAds'];
	    $model->isNewRecord = FALSE;

	    //print_r($_POST['GoogleAds']); die();
	    if ($model->save()) {
		Yii::app()->user->setFlash('success', 'The google ads has been updated');
	    } else {
		Yii::app()->user->setFlash('error', 'Sorry, please try again');
	    }
	}

	$this->render('update', array(
	    'model' => $model
	));
    }

    public function actionDelete($id) {
	if (!Yii::app()->request->isPostRequest)
	    throw new CHttpException(404, 'The page you has been requested not exists');

	$model = $this->_loadModel($id);
	$model->isNewRecord = FALSE;
	$model->delete();
    }

    public function actionIndex() {
	$model = new GoogleAds('search');

	if (isset($_GET['GoogleAds']))
	    $model->attributes = $_GET['GoogleAds'];

	$this->render('index', array(
	    'model' => $model
	));
    }

    private function _loadModel($id) {
	$model = GoogleAds::model()->findByPk($id);
	if (!$model)
	    throw new CHttpException(404, 'The page you has been requested not exists');

	return $model;
    }

}

